/*
 * Copyright (c) 2010-2011 Josef Hardi <josef.hardi@gmail.com>
 * Copyright (c) 2010 Piotr Zagawa <piotr.zagawa@gmail.com>
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package id.duapuluhdua.dakidaki;

import android.content.Context;
import android.widget.Toast;

import id.duapuluhdua.dakidaki.activities.ActivityAbout;
import id.duapuluhdua.dakidaki.activities.ActivityPreferences;
import id.duapuluhdua.dakidaki.common.CommonActivity;
import id.duapuluhdua.dakidaki.data.common.Database;

/**
 *
 */
public class UserAction
{
  /** Constants */
  public final static String WEB_PAGE = "";

  public final static String TRACK_VISIBLE = "visible";
  public final static String TRACK_RECORDING = "recording";

  /** Fields */
  private CommonActivity mParent;

  private static int lastCmd = Command.CMD_NONE;  // default

  /**
   * Default constructor.
   *
   * @param parent
   */
  public UserAction(CommonActivity parent)
  {
    mParent = parent;
  }

  /**
   * Retrieves the last called command.
   *
   * @return Returns the command key ID.
   */
  public synchronized int getLastCommand()
  {
    return lastCmd;
  }

  /**
   *
   * @param id
   * @return
   */
  public synchronized boolean run(int id)
  {
    return run(id, new Request(Request.MODE_NONE));
  }

  /**
   *
   * @param id
   * @param data
   * @return
   */
  public synchronized boolean run(int id, Request data)
  {
    Command key = Command.get(id);
    if (key != null) {
      return run(key, data);
    }
    return false;
  }

  /**
   *
   * @param key
   * @param data
   * @return
   */
  public synchronized boolean run(Command key, Request data)
  {
    // Assign this command key as the last called command.
    lastCmd = key.id;

    switch (key.id) {
      case Command.CMD_TRACK_HISTORY : return displayTrackHistory(key, data);
      case Command.CMD_TRACK_DETAILS : return displayTrackDetails(key, data);
      case Command.CMD_PREFERENCES : return displayPreferences(key, data);
      case Command.CMD_TRACK_START : return displayTrackStart(key, data);
      case Command.CMD_TRACK_SAVE : return displayTrackSave(key, data);
      case Command.CMD_TRACK_EDIT : return displayTrackEdit(key, data);
      case Command.CMD_TRACK_DELETE : return displayTrackDelete(key, data);
      case Command.CMD_TRACK_PAUSE : return displayTrackPause(key, data);
      case Command.CMD_TRACK_RESUME : return displayTrackResume(key, data);
      case Command.CMD_TRACK_STOP : return displayTrackStop(key, data);
      case Command.CMD_TRACK_SHOW : return displayTrackShow(key, data);
      case Command.CMD_TRACK_HIDE : return displayTrackHide(key, data);
      case Command.CMD_ABOUT : return displayAbout(key, data);
      case Command.CMD_DATA_EXPORT_TRACK : return displayDataExportTrack(key, data);
      case Command.CMD_MODE_MAP : return displayModeMap(key, data);
      case Command.CMD_MODE_TRACKS : return displayModeTracks(key, data);
      case Command.CMD_MODE_INFO : return displayModeInfo(key, data);
    }
    return false; // if find nothing.
  }

  /**
   * @param key
   * @param data
   * @return
   */
  private boolean displayTrackStart(Command key, Request data)
  {
//    final Database database = mParent.getDatabase();
//    final DataValues values = database.getTableTracks().getNewValues(true); // insert
//
//    boolean bSuccess = false;
//    long lTrackId = -1;
//
//    database.transactionBegin();
//    try {
//      lTrackId = database.getTableTracks().dataInsert(values);
//      if (lTrackId != -1) {
//        database.transactionCommit();
//        bSuccess = true;
//      }
//      else {
//        UserAlert.show(mParent, UserAlert.Result.DATA_INSERT_ERROR);
//      }
//    }
//    finally {
//      database.transactionEnd();
//    }
//
//    return bSuccess;

    return true;
  }

  private boolean displayTrackSave(Command cmd, Request data)
  {
    final Database database = mParent.getDatabase();
    final long trackId = mParent.getMainState().getSelectedTrack();

    boolean bSuccess = false;

    database.transactionBegin();
    try {
      if (trackId != -1) {
        if (database.getTableTracks().pauseRecording(trackId, true)) {
          database.transactionCommit();
          bSuccess = true;
        }
        else {
          UserAlert.show(mParent, UserAlert.Result.CLOSE_TRACK_ERROR);
        }
      }
      else {
        UserAlert.show(mParent, UserAlert.Result.DATA_FIND_ERROR);
      }
    }
    finally {
      database.transactionEnd();
    }
    return bSuccess;
  }

  private boolean displayTrackEdit(Command cmd, Request data)
  {
//    data = CommandData.setModeEdit(data);
//    mParent.showActivity(ActivityTrackSave.class, cmd.iId, data);
    return true;
  }

  private boolean displayTrackDelete(Command cmd, Request data)
  {
    final long rowId = data.getRowId();
    if (rowId == -1) {
      UserAlert.show(mParent, UserAlert.Result.MORE_DATA_REQUIRED);
      return false;
    }
    return mParent.getDatabase().getTableTracks().deleteTrack(rowId);
  }

  private boolean displayTrackPause(Command cmd, Request data)
  {
//    mParent.getIntent().getExtras().putBoolean(TRACK_RECORDING, false);
//
//    final long lRowId = data.getRowId();
//    if (lRowId == -1) {
//      UserAlert.show(mParent, UserAlert.Result.MORE_DATA_REQUIRED);
//      return false;
//    }
//
//    return mParent.getDatabase().getTableTracks().pauseRecording(lRowId, false);

    return true;
  }

  private boolean displayTrackResume(Command cmd, Request data)
  {
    mParent.getIntent().getExtras().putBoolean(TRACK_RECORDING, true);
    return true;
  }

  private boolean displayTrackStop(Command cmd, Request data)
  {
    mParent.getIntent().getExtras().putBoolean(TRACK_RECORDING, false);

    final long rowId = data.getRowId();
    if (rowId == -1) {
      UserAlert.show(mParent, UserAlert.Result.MORE_DATA_REQUIRED);
      return false;
    }
    return mParent.getDatabase().getTableTracks().pauseRecording(rowId, true);
  }

  private boolean displayTrackShow(Command cmd, Request data)
  {
    mParent.getIntent().getExtras().putBoolean(TRACK_VISIBLE, true);
    return true;
  }

  private boolean displayTrackHide(Command cmd, Request data)
  {
//    mParent.getIntent().getExtras().putBoolean(TRACK_VISIBLE, false);
    return true;
  }

  private boolean displayTrackHistory(Command cmd, Request data)
  {
//    mParent.showActivity(ActivityTrackHistory.class, cmd.iId, data);
    return true;
  }

  private boolean displayTrackDetails(Command cmd, Request data)
  {
//    mParent.showActivity(ActivityTrackDetails.class, cmd.iId, data);
    return true;
  }

  private boolean displayPreferences(Command cmd, Request data)
  {
    mParent.showActivity(ActivityPreferences.class, cmd.id, data);
    return false;
  }

  private boolean displayAbout(Command cmd, Request data)
  {
    mParent.showActivity(ActivityAbout.class, cmd.id, data);
    return true;
  }

  private boolean displayDataExportTrack(Command cmd, Request data)
  {
//    if (data != null)
//      ActivityExport.setExportTypeTrack(data);
//
//    mParent.showActivity(ActivityExport.class, CommandKey.CMD_DATA_EXPORT_TRACK,
//        data);
    return false;
  }

  private boolean displayModeMap(Command cmd, Request data)
  {
    mParent.getMainState().changeUiMode(Command.CMD_MODE_MAP);

    final int currentMode = mParent.getMainState().getCurrentUiMode();
    final Command cmdMode = Command.get(currentMode);
    if (cmdMode != null) {
      showNotificationMessage(cmdMode.name);
      displayTrackHide(cmd, data);
      return true;
    }
    return false;
  }

  private boolean displayModeTracks(Command cmd, Request data)
  {
    mParent.getMainState().changeUiMode(Command.CMD_MODE_TRACKS);

    final int currentMode = mParent.getMainState().getCurrentUiMode();
    final Command cmdMode = Command.get(currentMode);
    if (cmdMode != null) {
      showNotificationMessage(cmdMode.name);
      return true;
    }
    return false;
  }

  private boolean displayModeInfo(Command cmd, Request data)
  {
    mParent.getMainState().changeUiMode(Command.CMD_MODE_INFO);

    final int currentMode = mParent.getMainState().getCurrentUiMode();
    final Command cmdMode = Command.get(currentMode);
    if (cmdMode != null) {
      showNotificationMessage(cmdMode.name);
      return true;
    }
    return false;
  }

  /**
   * @param text
   */
  private void showNotificationMessage(String text)
  {
    final Context context = mParent.getApplicationContext();

    Toast toast = Toast.makeText(context, text, Toast.LENGTH_SHORT);
    toast.show();
  }
}
